//
//  NSString+Date.m
//  HouseManage
//
//  Created by 翁胜琼 on 18/11/1.
//  Copyright © 2016年 cn.devjohn . All rights reserved.
//

#import "NSString+Date.h"
#import "NSDate+DateFormat.h"

@implementation NSString (Date)

// MARK:___________________________________________________________________________________________

+ (NSString *)getMonthDayStringWithTimestamp:(NSInteger)timestamp
{
    NSString *dateString = [self getDateStringFromTimestamp:timestamp with:@"MM月dd日"];
    
    return dateString;

}

// 获取年月 2019年01月
+ (NSString *)getYearMonthStringWithTimestamp:(NSInteger)timestamp
{
    NSString *dateString = [self getDateStringFromTimestamp:timestamp with:@"YYYY年MM月"];
    
    return dateString;
}

// 2018-10-11 10:23:23
+ (NSString *)getDateTimeWithTimestamp:(NSInteger)timestamp
{
    NSString *dateString = [self getDateStringFromTimestamp:timestamp with:@"YYYY-MM-dd HH:mm:ss"];
    
    return dateString;
}

// 2018-10-11
+ (NSString *)getDateWithTimestamp:(NSInteger)timestamp
{
    NSString *dateString = [self getDateStringFromTimestamp:timestamp with:@"YYYY-MM-dd"];
    
    return dateString;
}

// 获取当前的时间戳
+ (NSTimeInterval)getCurrentTimeinterval
{
    NSDate *now = [NSDate date];
    NSTimeInterval timeinterval = [now timeIntervalSince1970];
    
    return timeinterval;
}

// 指定时间戳转化方法,默认时区上海
+ (NSString *)getDateStringFromTimestamp:(NSInteger)timestamp with:(NSString *)dateFormatterString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:dateFormatterString];
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSString *dateTimeString = [formatter stringFromDate:date];

    return dateTimeString;
}

// 返回当前时间 YYYY-MM-dd
+ (NSString *)getCurrentDate
{
    NSTimeInterval timeinterval = [self getCurrentTimeinterval];
    NSString *currentDate = [self getDateWithTimestamp:timeinterval];
    return currentDate;
}

//传入指定的日期，返回指定的日期
+ (NSString *)getBeginningDate:(NSDate *)aDate
                  dateInterval:(NSInteger)interval
                     dateFormat:(NSString *)dateFormat
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
    [components setDay:([components day]+interval)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
    [dateday setDateFormat:dateFormat];
    return [dateday stringFromDate:beginningOfWeek];
}
@end
